﻿using System;
using GameEngine.Model;
using GameEngine.Model.Data;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using GameEngine.View.Components;

namespace GameEngine.Controller
{
    public class RemoveThingFromWorldCommand<MyIdType, MyBaseUIElementType> : SimpleCommand
        where MyIdType : IComparable<MyIdType>
    {
        override public void Execute(INotification notification)
        {
            var thing = notification.Body as IThingVOId<MyIdType>;
            var worldId = notification.Type;

            foreach (var lvl in ModelFacade<MyIdType>.Instance.Game.Levels.Values) {
                foreach (var world in lvl.Worlds) { 
                    if(world.Id.Value.Equals(worldId)){
                        var thingDataProxy = ModelFacade<MyIdType>.Helper.GetThingDataProxy(thing);
                        var worldDataProxy = ModelFacade<MyIdType>.Helper.GetWorldDataProxy(world);
                        
                        worldDataProxy.RemoveItem(thingDataProxy);
                        
                        ApplicationFacadeHelper<MyIdType>.DestroyThing(thing);
                        
                        //var worldThingMediator = ViewFacade<MyGameView, MyLevelView, MyWorldView, MyMapView, MyThingView, MyIdType, MyBaseUIElementType>.Helper.GetWorldThingMediator(world.Id);
                        //var viewInstance = worldThingMediator.GetThingDataProxyView(thingDataProxy);
                        //ApplicationFacadeHelper<MyIdType>.DestroyView(viewInstance.);
                        
                        return;
                    }
                }
            }
        }
    }
}
